public class Solution206 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    public ListNode reverseList(ListNode head) {
        if (head==null || head.next==null){
            return head;
        }
        ListNode f=head;
        ListNode last=head;
        ListNode cur=head.next;
        while (cur!=null){
            ListNode t=cur.next;
            cur.next=last;
            last=cur;
            cur=t;
        }
        f.next=null;
        return last;
    }

    public static void main(String[] args) {
        System.out.println(new Solution206().reverseList(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5)))))).next.val);
    }
}
